﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BioService.Database.Abstract;
using BioService.Database.Models;

namespace BioService.Database.Context
{
    public class BioDatabaseRepository : IBioDatabaseRepository
    {
        private readonly Models.Context context;

        public BioDatabaseRepository()
        {
            context = new Models.Context();
        }

        public IQueryable<User> Users
        {
            get { return context.Users; }
        }

        public IQueryable<PaypalInfo> PaypalInfos
        {
            get { return context.PaypalInfos; }
        }

        public void Save(User user)
        {
            if (user.UserID == 0)
                context.Users.Add(user);
            context.SaveChanges();
        }

        public void Save(PaypalInfo paypal)
        {
            if (paypal.PaypalInfoID == 0)
                context.PaypalInfos.Add(paypal);
            context.SaveChanges();
        }

        public void Delete(User user)
        {
            if (context.Users.FirstOrDefault(x => x.UserID == user.UserID) != null)
                context.Users.Remove(user);
            context.SaveChanges();
        }

        public void Delete(PaypalInfo paypal)
        {
            if (context.PaypalInfos.FirstOrDefault(x => x.PaypalInfoID == paypal.PaypalInfoID) != null)
                context.PaypalInfos.Remove(paypal);
            context.SaveChanges();
        }
    }
}